<?php
/**
 * Created by PhpStorm.
 * User: River
 * Email: river0428@gmail.com
 * Date: 2018/11/15
 * Time: 下午2:28
 */

namespace Modules\Admin\Main\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Coupon;
use App\Models\Recharge;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class CouponController extends Controller
{
    public function index()
    {
        \Request::merge([
            'recharge' => Recharge::get(['id', 'title', 'only_first', 'discount', 'months']),
        ]);
        return view('main::coupon', \Request::all());
    }

    public function all()
    {
        $data['items'] = Coupon::orderBy('id', 'desc')->with('recharge')->get();

        return success($data);
    }

    public function store(Request $request)
    {
        $data                  = $request->only([
            'title',
            'type',
            'channel',
            'amount',
            'expire_days',
            'started_at',
            'finished_at',
            'recharge_id',
            'only_recharge',
            'expire_type',
            'can_receive',
            'once_limit',
            'total_limit',
            'min_amount',
            'months'
        ]);
        $data['only_recharge'] = 1;

        $validator = Validator::make($request->all(), [
            'type'        => 'required',
            'expire_type' => 'required',
            'title'       => 'required',
        ]);
        if ($validator->fails()) {
            return fail(400, $validator->errors()->first());
        }

        if ($data['expire_type'] == Coupon::EXPIRE_TYPE1 && (!isset($data['expire_days']) OR !$data['expire_days'])) {
            return fail(400, '有效天数不能为空');
        }
        if ($data['expire_type'] == Coupon::EXPIRE_TYPE2 &&
            (!isset($data['started_at']) OR !$data['started_at'] OR
                !isset($data['finished_at']) OR !$data['finished_at'])
        ) {
            return fail(400, '有效期不能为空');
        }

        Coupon::create($data);
        $this->recordAction('添加了新的优惠券<' . $data['title'] . '>',$data);
        return success();
    }

    public function update(Request $request, $id)
    {
        $res = Coupon::find($id);
        try {

            if ($request->has('title') && $request->input('title')) {
                $res->title = $request->input('title');
            }
            if ($request->has('channel') && $request->input('channel')) {
                $res->channel = $request->input('channel');
            }

            if ($request->has('amount')) {
                $res->amount = $request->input('amount');
            }
            if ($request->has('expire_days')) {
                $res->expire_days = $request->input('expire_days');
            }
            if ($request->has('recharge_id') && $request->input('recharge_id')) {
                $res->recharge_id = $request->input('recharge_id');
            }
            if ($request->has('started_at') && $request->input('started_at')) {
                $res->started_at = $request->input('started_at');
            }
            if ($request->has('finished_at') && $request->input('finished_at')) {
                $res->finished_at = $request->input('finished_at');
            }

            if ($request->has('can_receive')) {
                $res->can_receive = $request->input('can_receive');
            }
            if ($request->has('once_limit')) {
                $res->once_limit = $request->input('once_limit');
            }
            if ($request->has('total_limit')) {
                $res->total_limit = $request->input('total_limit');
            }
            if ($request->has('min_amount')) {
                $res->min_amount = $request->input('min_amount');
            }
            if ($request->has('only_recharge')) {
                $res->only_recharge = $request->input('only_recharge');
            }
            if ($request->has('months')){
                $res->months = $request->input('months');
            }

            $res->save();
            $this->recordAction('更新了优惠券<' . $id . '>',$res->toArray());
            return success();

        } catch (\Exception $e) {
            Log::error($e);
            return fail(500, '系统出错了，请稍后再试');
        }
    }

    public function destroy($id)
    {
        $res = Coupon::find($id);
        $res->delete();
        $this->recordAction('删除了优惠券<' . $id . '>',$res->toArray());
        return success();
    }
}